#include "StdAfx.h"
#include "GraphicControlMedia.h"
#include "MediaObject.h"
#include "MultiMediaDlg.h"

#include <string>
#define MY_TIMEREVENT 9991

IMPLEMENT_DYNAMIC(GraphicControlMedia,CWnd)

GraphicControlMedia::GraphicControlMedia(void)
{

}

GraphicControlMedia::~GraphicControlMedia(void)
{

}
BEGIN_MESSAGE_MAP(GraphicControlMedia, CWnd)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BTN_FULL_SCREEN,OnBnClickedFullScreen)
	ON_BN_CLICKED(IDC_BTN_PLAY_PAUSE,OnBnClickedPlayPause)
	ON_BN_CLICKED(IDC_BTN_STOP,OnBnClickedStop)
	ON_BN_CLICKED(IDC_BTN_NEXT,OnBnClickedNext)
	ON_BN_CLICKED(IDC_BTN_PREVIOUS,OnBnClickedPrevious)
	ON_BN_CLICKED(IDC_CKB_MUTE,OnBnClickedMute)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_SEEK_BAR, OnNMReleasedcaptureSldSeekBar)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLD_VOLUME, OnNMReleasedcaptureSldVolume)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

GraphicControlMedia *gInstance;
GraphicControlMedia* GraphicControlMedia::ShareInstance()
{
	return gInstance;
}
int GraphicControlMedia::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	gInstance = this;
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO:  Add your specialized creation code here
	m_SliderSeeking.Create(_T(""),WS_CHILD | WS_VISIBLE | TBS_HORZ, CRect(0, 0, 900, 30), this, IDC_SLD_SEEK_BAR);
	m_SliderSeeking.ShowWindow(SW_SHOW);

	m_stcTimePlaying.Create(_T("00:00:00"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this, NULL);
	m_stcTimePlaying.ShowWindow(SW_SHOW);

	m_stcTab.Create(_T("/"),WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this,NULL);
	m_stcTab.ShowWindow(SW_SHOW);

	m_stcTimeTotal.Create(_T("00:00:00"), WS_CHILD | WS_VISIBLE, CRect(0, 0, 80, 30), this,NULL);
	m_stcTimeTotal.ShowWindow(SW_SHOW);

	m_btncheckLoop.Create(_T("Loop"),WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,CRect(0,0,60,30),this,NULL);
	m_btncheckLoop.ShowWindow(SW_SHOW);

	m_btnPlayPause.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PLAY_PAUSE);
	m_btnPlayPause.ShowWindow(SW_SHOW);
	m_btnPlayPause.LoadStdImage(IDB_PLAY, IDB_PLAY, IDB_PLAY,_T("PNG"));
	m_btnPlayPause.LoadAltImage(IDB_PAUSE, _T("PNG"));
	m_btnPlayPause.EnableToggle(TRUE);

	m_btnStop.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_STOP);
	m_btnStop.ShowWindow(SW_SHOW);
	m_btnStop.LoadStdImage(IDB_STOP, IDB_STOP, IDB_STOP, _T("PNG"));

	m_btnNext.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_NEXT);
	m_btnNext.ShowWindow(SW_SHOW);
	m_btnNext.LoadStdImage(IDB_NEXT, IDB_NEXT, IDB_NEXT, _T("PNG"));

	m_btnPrevious.Create(NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_PREVIOUS);
	m_btnPrevious.ShowWindow(SW_SHOW);
	m_btnPrevious.LoadStdImage(IDB_PREVIOUS, IDB_PREVIOUS, IDB_PREVIOUS, _T("PNG"));

	m_btnFullScreen.Create(_T("Full Screen"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,100,30),this,IDC_BTN_FULL_SCREEN);
	m_btnFullScreen.ShowWindow(SW_SHOW);

	//m_btncheckmute.Create(_T("Mute"),WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX,CRect(0,0,60,30),this,IDC_CKB_MUTE);
	m_btncheckmute.Create(_T("Mute"),WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,CRect(0,0,60,30),this,IDC_CKB_MUTE);
	m_btncheckmute.ShowWindow(SW_SHOW);
	m_btncheckmute.LoadStdImage(IDB_VOLUME, IDB_VOLUME, IDB_VOLUME, _T("PNG"));
	m_btncheckmute.LoadAltImage(IDB_VOLUME_MUTE, _T("PNG"));

	/*m_SliderVolume.Create(WS_CHILD|WS_VISIBLE|TBS_HORZ,CRect(0,0,0,30),this,IDC_SLD_VOLUME);
    m_SliderVolume.ShowWindow(SW_SHOW);*/
	
	m_SliderSeeking.SetBitmapChannel(IDB_TRACK_NORMAL, IDB_TRACK_ACTIVE);
	m_SliderSeeking.SetBitmapThumb(IDB_THUMB, IDB_THUMB, TRUE);
	m_SliderSeeking.DrawFocusRect(FALSE);
	m_SliderSeeking.SetRange(MIN_RANGE_SEEK_SLIDER, MAX_RANGE_SEEK_SLIDER);
	//m_SliderVolume.SetRange(MIN_RANGE_VOL_SLIDER, MAX_RANGE_VOL_SLIDER);
	
	//CMultiMediaDlg::ShareInstance()->m_nVol = -1;
	//m_SliderVolume.SetPos(CMultiMediaDlg::ShareInstance()->m_nVol);
	//m_nVol = -1;
	//m_SliderVolume.SetPos(-1);
	
	return 0;
}

void GraphicControlMedia::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
    CWnd *parent = GetParent();
	CRect rect;
	this->GetClientRect(&rect);
	parent->GetClientRect(&rect);
	CRect rtViewArea(0,rect.bottom-rect.top-150,rect.right, rect.bottom);//here
   

	int nMarginLeft = (rtViewArea.right - rtViewArea.left )/3;
	int nMarginTop  = (rtViewArea.bottom - rtViewArea.top );
	int width = rtViewArea.right - rtViewArea.left;

	m_SliderSeeking.SetWindowPos(parent,	0,									 nMarginTop - 150, rtViewArea.right,		30, SWP_NOZORDER);
	m_stcTimePlaying.SetWindowPos(parent,	nMarginLeft / 8,					 nMarginTop - 115, 70,						30, SWP_NOZORDER);
	m_stcTab.SetWindowPos(parent,			nMarginLeft / 8 + 70,				 nMarginTop - 115, 5,						30, SWP_NOZORDER);
	m_stcTimeTotal.SetWindowPos(parent,		nMarginLeft / 8 + 80,				 nMarginTop - 115, 70,						30, SWP_NOZORDER);
	m_btncheckLoop.SetWindowPos(parent,		4 * nMarginLeft / 8 + 30,			 nMarginTop - 120, 80,						30, SWP_NOZORDER);
	/*m_btnPlayPause.SetWindowPos(parent,		nMarginLeft - nMarginLeft / 8,		 nMarginTop - 120, 48,						48, SWP_NOZORDER);
	m_btnStop.SetWindowPos(parent,			nMarginLeft - nMarginLeft / 8 + 110, nMarginTop - 120, 40,						40, SWP_NOZORDER);
    m_btnNext.SetWindowPos(parent,			nMarginLeft - nMarginLeft / 8 + 220, nMarginTop - 120, 40,						40, SWP_NOZORDER);
	m_btnPrevious.SetWindowPos(parent,		nMarginLeft - nMarginLeft / 8 + 330, nMarginTop - 120, 40,						40, SWP_NOZORDER);
	m_btnFullScreen.SetWindowPos(parent,	nMarginLeft - nMarginLeft / 8 + 440, nMarginTop - 120, 100,						30, SWP_NOZORDER);
	m_btncheckmute.SetWindowPos(parent,		nMarginLeft - nMarginLeft / 8 + 590, nMarginTop - 120, 70,						30, SWP_NOZORDER);
	m_SliderVolume.SetWindowPos(parent,		nMarginLeft - nMarginLeft / 8 + 660, nMarginTop - 120, 4 * nMarginLeft / 8,		30, SWP_NOZORDER);*/
	m_btnPlayPause.SetWindowPos(parent, width / 2, nMarginTop - 120, 48, 48, SWP_NOZORDER);
	m_btnNext.SetWindowPos(parent, width / 2 + 48, nMarginTop - 115, 40, 40, SWP_NOZORDER);
	m_btnPrevious.SetWindowPos(parent, width / 2 - 40, nMarginTop - 115, 40, 40, SWP_NOZORDER);
	m_btnStop.SetWindowPos(parent, width / 2 + 100, nMarginTop - 115, 40, 40, SWP_NOZORDER);
	m_btncheckmute.SetWindowPos(parent, width - 100, nMarginTop - 115, 40, 40, SWP_NOZORDER);
}

void GraphicControlMedia::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages
}

BOOL GraphicControlMedia::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//CRect rect;

	//GetClientRect(&rect);
	//CBrush myBrush(COLOR_VIEW_AREA);    // dialog background color
	//CBrush *pOld = pDC->SelectObject(&myBrush);
	//BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
	//pDC->SelectObject(pOld);    // restore old brush
	//return bRes;   

	return CWnd::OnEraseBkgnd(pDC);
}
void GraphicControlMedia::OnBnClickedFullScreen()
{
	LONGLONG cur = 0;
	KillTimer(MY_TIMEREVENT);
	//SetTimer(TIME_EVENT,1000,(TIMERPROC)NULL);// set timer 1s
	CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaSeeking->GetCurrentPosition(&cur);
	m_fullScreen.m_currentPos = cur;
	m_fullScreen.m_path =  CMultiMediaDlg::ShareInstance()->m_strFilePath; 
	//m_fullScreen.m_lVolume =(long)m_sliderVolume.GetPos()*1000;
	//m_isFullScreen = 1;
	CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Pause();

	//dlgSubtitleScreen->ShowWindow(FALSE);
	ShowWindow(FALSE);
	

	int tt = m_fullScreen.DoModal();
	if (tt == IDD_FULL_VIDEO_SCREEN){
		//MediaStop();
		CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Stop();
		
	}
	else {
		cur = m_fullScreen.m_currentPos;
		SetTimer(MY_TIMEREVENT,1000,(TIMERPROC)NULL);// set timer 1s
		CMultiMediaDlg::ShareInstance()->m_VideoMng.pMediaControl->Run();
		LONGLONG lTimeLineUnit =0;
		lTimeLineUnit=CMultiMediaDlg::ShareInstance()->m_VideoMng.m_length/MAX_RANGE_SEEK_SLIDER; //slide range[0-100]
		int nPos = (int)cur/lTimeLineUnit;
		//m_sliderSeek.SetPos(nPos);
		CMultiMediaDlg::ShareInstance()->m_VideoMng.SetPosition2(cur);
		//dlgSubtitleScreen->ShowWindow(TRUE);
		
	}
	
	this->ShowWindow(TRUE);	
}

void GraphicControlMedia::OnBnClickedPlayPause()
{
	if(CMultiMediaDlg::ShareInstance()->m_bIsStop)
	{
		CMultiMediaDlg::ShareInstance()->MediaPlay();
		m_btnNext.EnableButton(FALSE);
		m_btnNext.EnableWindow(FALSE);
		CMultiMediaDlg::ShareInstance()->m_bIsStop = false;
	}
	else
	{
		if(CMultiMediaDlg::ShareInstance()->m_bIsPlaying)
		{
			CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Pause();
			CMultiMediaDlg::ShareInstance()->m_bIsPlaying = false;
		}
		else
		{
			CMultiMediaDlg::ShareInstance()->m_VideoMng.MultiMedia_Resume();
			CMultiMediaDlg::ShareInstance()->m_bIsPlaying = true;
		}
	}
}

void GraphicControlMedia::OnBnClickedStop()
{
	CMultiMediaDlg::ShareInstance()->MediaStop();
}

void GraphicControlMedia::OnBnClickedNext()
{
	CMultiMediaDlg::ShareInstance()->NextOrPrevPlayList(true);
}

void GraphicControlMedia::OnBnClickedPrevious()
{
	CMultiMediaDlg::ShareInstance()->NextOrPrevPlayList(false);
}

void GraphicControlMedia::OnNMReleasedcaptureSldSeekBar(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	CString strTime = CMultiMediaDlg::ShareInstance()->m_VideoMng.SetPosition(m_SliderSeeking.GetPos());
	m_stcTimePlaying.SetWindowTextW((LPCTSTR)strTime);
	if(m_SliderSeeking.GetPos() == MAX_RANGE_SEEK_SLIDER)
	{
	}
	else
	{
	}
}

void GraphicControlMedia::OnNMReleasedcaptureSldVolume(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;
	//CMultiMediaDlg::ShareInstance()->m_nVol = m_SliderVolume.GetPos();
	//CMultiMediaDlg::ShareInstance()->MediaVolume(CMultiMediaDlg::ShareInstance()->m_nVol);

}

void GraphicControlMedia::OnBnClickedMute()
{
	/*if(m_btncheckmute.GetCheck())
	{
		CMultiMediaDlg::ShareInstance()->m_nVol = MIN_RANGE_VOL_SLIDER;
	}
	else
		CMultiMediaDlg::ShareInstance()->m_nVol = m_SliderVolume.GetPos();
	CMultiMediaDlg::ShareInstance()->MediaVolume(CMultiMediaDlg::ShareInstance()->m_nVol);*/
	//bool check= false;
	//if(check==false)
	//{
	//SliderVolumeDlg sldVolumeDlg;
	//if (sldVolumeDlg.DoModal() == IDOK)
	//{
	//  check=true;
	//}
	//else
	//{
	//	//EndDialog(sldVolumeDlg.GetSafeHwnd(),IDD_SLDVOLUME);
	//	//SendMessage(WM_CLOSE);

	//}
}
